package work.linruchang.oauthresourceproject.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import work.linruchang.oauthresourceproject.config.interceptor.BearerAuthInterceptor;
import work.linruchang.oauthresourceproject.config.interceptor.ClientBasicAuthInterceptor;
import work.linruchang.oauthresourceproject.config.interceptor.SystemUserBasicAuthInterceptor;

/**
 * 自定义Spring的配置
 *
 * @author LinRuChang
 * @version 1.0
 * @date 2022/08/04
 * @since 1.8
 **/
@Configuration
public class MyConfig implements WebMvcConfigurer {

    @Autowired
    SystemUserBasicAuthInterceptor systemUserBasicAuthInterceptor;
    @Autowired
    ClientBasicAuthInterceptor clientBasicAuthInterceptor;
    @Autowired
    BearerAuthInterceptor bearerAuthInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        registry.addInterceptor(systemUserBasicAuthInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns("/js/**","/oauth/token");

        registry.addInterceptor(clientBasicAuthInterceptor)
                .addPathPatterns("/oauth/token");

        registry.addInterceptor(bearerAuthInterceptor)
                .addPathPatterns("/user/user_info");

    }

    /**
     * 首页重定向要用户信息页面
     * @param registry
     */
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addRedirectViewController("/", "/user");
    }
}
